package Game.Items.Recipes;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import com.sun.xml.internal.fastinfoset.algorithm.IEEE754FloatingPointEncodingAlgorithm;

import GUI.GuiManager;
import GUI.GuiMessenger;
import Game.Statistics;
import Game.Items.GameItem;
import Game.Objects.GOPlayer;
import GameMath.GPoint;
import Screens.MyScreens.GameScreen;
import Tools.SoundManager;
import Tools.Drawing.FontTagSystem;

/** Base recipe class
 * @author Aikidos
 */
public class GIRecipe extends GameItem {
	
	/** List of item class name for craft */
	protected Map<GameItem, Integer> _lstItemsForCraft  = new HashMap<GameItem, Integer>();
	
	/** Magic for craft */
	protected int _magic = 0;
	
	/** Item class */
	private GameItem _resultItem; 
	
	public GIRecipe(String itemName, GameItem resultItem) {
		super("Recipe: " + itemName);
		
		_resultItem = resultItem;
		_weight = 0.02f;
	}
	
	@Override
	public boolean use() throws SlickException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
		// Player
		if (_ownerGameObject.isPlayer()) {
			GOPlayer player = (GOPlayer) _ownerGameObject;

			// Near anvil?
			GPoint anvilCoord = player.getObjectManager().getObjectsByClass("Game.Objects.GOAnvil")[0].getCoord();
			if ((anvilCoord.x == player.getCoord().x)
					&& (anvilCoord.y == player.getCoord().y)) {
			
			if (player.Magic >= _magic) {
				player.Magic -= _magic;
				
					// Create list for save found items
					List<GameItem> lstFound = new ArrayList<GameItem>();

					for (Entry<GameItem, Integer> entry : _lstItemsForCraft
							.entrySet()) {
						int count = entry.getValue();

						for (GameItem item : player.getInventory().getItems()) {
							if (item.getClass().getName() == entry.getKey().getClass().getName()) {
								if (item.isStackable()) {

									for (int i = 0; i < count; i++)
										lstFound.add(item);

									count -= item.getNumOfItems();

								} else {
									lstFound.add(item);

									count--;
								}
							}
						}

						if (count > 0)
							return false;
					}

					// Delete items
					for (GameItem item : lstFound) {
						player.getInventory().deleteItem(item, true);
					}

					// Add item
					player.getInventory().addItem(_resultItem);
					
					// Remove from unknown list
					Statistics.removeUnknownItem(_resultItem);

					// Show message
					GuiManager.getMessenger().addMessage("[green]You[/green] created a [yellow]"
									+ _resultItem.getName() + "[/yellow]");

					// Play sound
					SoundManager.play("sfx_anvil", 0.2f);
					
					return true;

				} else return false;
			}
		}
		
		return false;
	}

	@Override
	public void drawInfo(Vector2f drawPos, Graphics g) {
		FontTagSystem.drawText("Required items:", drawPos);
		drawPos.y += 20;
		
		for (Entry<GameItem, Integer> entry : _lstItemsForCraft
				.entrySet()) {
			FontTagSystem.drawText("[yellow]" + entry.getKey().getName() + "[/yellow] x" +
				entry.getValue(), drawPos);
			
			drawPos.y += 20;
		}
		
		drawPos.y += 20;
		
		FontTagSystem.drawText("Required magic: [cyan]" + String.valueOf(_magic) + "[/cyan]", drawPos);
	}
	
	
	
}
